package com.xyy.saas.payment.converter;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;

import com.xyy.saas.payment.cores.enums.ChannelEnum;
import com.xyy.saas.payment.cores.enums.ComEnums;
import com.xyy.saas.payment.dao.model.LoanRepayPlanDetailPo;
import com.xyy.saas.payment.dao.model.LoanRepayPlanPo;
import com.xyy.saas.payment.dao.model.OrderTransDataPo;
import com.xyy.saas.payment.dto.OrderTransDataQo;
import com.xyy.saas.payment.pojo.vo.JdCreditBIllListReq;
import com.xyy.saas.payment.pojo.vo.JdCreditBIllListVo;
import com.xyy.saas.payment.pojo.vo.JdCreditBIllRepayDetailVo;
import com.xyy.saas.payment.pojo.vo.JdCreditBillRepayVo;
import com.xyy.saas.payment.util.AmountCastUtil;
import com.xyy.saas.payment.util.NumberUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author liujf
 */
public class JdCreditRepayConverter {
    public static List<JdCreditBIllListVo> toBill(List<OrderTransDataPo> transDataPoList) {
        if (CollUtil.isEmpty(transDataPoList)) {
            return Collections.emptyList();
        }
        List<JdCreditBIllListVo> bIllListVos = new ArrayList<>(transDataPoList.size());
        for (OrderTransDataPo transDataPo : transDataPoList) {
            JdCreditBIllListVo bIllListVo = new JdCreditBIllListVo();
            bIllListVo.setLoanNo(transDataPo.getChannelTransactionNo());
            bIllListVo.setBusinessOrderNo(transDataPo.getBusinessOrderNo());
            bIllListVo.setBusinessRefundNo(transDataPo.getBusinessRefundNo());
            bIllListVo.setPayNo(transDataPo.getPayNo());
            bIllListVo.setOrderType(ComEnums.BillTypeEnum.getNameByCode(transDataPo.getBillType()));
            bIllListVo.setSellerId(transDataPo.getSellerId());
            bIllListVo.setSellerName(transDataPo.getSellerName());
            bIllListVo.setCustName(transDataPo.getCustName());
            bIllListVo.setCustCode(transDataPo.getCustCode());
            if (transDataPo.getPaySuccessTime() != null) {
                bIllListVo.setPayTime(DatePattern.NORM_DATETIME_FORMAT.format(transDataPo.getPaySuccessTime()));
            }
            bIllListVo.setPayAmount(AmountCastUtil.castFen2YuanDecimal(transDataPo.getPayAmount()));
            bIllListVo.setPayCashAmount(AmountCastUtil.castFen2YuanDecimal(transDataPo.getPayCashAmount()));
            bIllListVo.setVirtualGoldAmount(AmountCastUtil.castFen2YuanDecimal(transDataPo.getVirtualGoldAmount()));
            bIllListVo.setRefundPayAmount(AmountCastUtil.castFen2YuanDecimal(transDataPo.getRefundPayAmount()));
            bIllListVo.setRefundPayCashAmount(AmountCastUtil.castFen2YuanDecimal(transDataPo.getRefundPayCashAmount()));
            bIllListVo.setRefundVirtualGoldAmount(AmountCastUtil.castFen2YuanDecimal(transDataPo.getRefundVirtualGoldAmount()));
            bIllListVos.add(bIllListVo);
        }
        return bIllListVos;
    }

    public static OrderTransDataQo req2qo(JdCreditBIllListReq req) {
        OrderTransDataQo transDataQo = new OrderTransDataQo();
        transDataQo.setChannelTransactionNo(req.getLoanNo());
        transDataQo.setBusinessOrderNo(req.getBusinessOrderNo());
        transDataQo.setPayNo(req.getPayNo());
        transDataQo.setCustCode(req.getCustCode());
        transDataQo.setCustName(req.getCustName());
        transDataQo.setSellerId(req.getSellerId());
        transDataQo.setSellerName(req.getSellerName());
        transDataQo.setStartPayTime(req.getStartPayDate());
        transDataQo.setEndPayTime(req.getEndPayDate());
        transDataQo.setPayChannel(ChannelEnum.JD_CREDIT.getCode());
        transDataQo.setBillType(req.getOrderType());
        return transDataQo;
    }

    public static JdCreditBillRepayVo repayPlan2vo(LoanRepayPlanPo repayPlanPo, List<LoanRepayPlanDetailPo> planDetailPos) {
        JdCreditBillRepayVo billRepayVo = new JdCreditBillRepayVo();
        billRepayVo.setLoanNo(repayPlanPo.getLoanNo());
        billRepayVo.setPayNo(repayPlanPo.getPayNo());
        billRepayVo.setBusinessOrderNo(repayPlanPo.getBusinessOrderNo());
        billRepayVo.setLoanStatus(ComEnums.LoanStatusEnum.getNameByCode(repayPlanPo.getLoanStatus()));
        billRepayVo.setPreSettleAmt(repayPlanPo.getPreSettleAmt());
        billRepayVo.setPaidAmt(repayPlanPo.getPaidAmt());
        billRepayVo.setDayRate(NumberUtil.decimal2rate(repayPlanPo.getDayRate()));
        billRepayVo.setYearRate(NumberUtil.decimal2rate(repayPlanPo.getYearRate()));
        billRepayVo.setLoanAmt(repayPlanPo.getLoanAmt());
        billRepayVo.setCurrUnPayAmt(repayPlanPo.getCurrUnPayAmt());
        if (repayPlanPo.getTotalPlan() != null) {
            billRepayVo.setTotalPlan(repayPlanPo.getTotalPlan() + "期");
        }
        billRepayVo.setDueUnPayAmt(repayPlanPo.getDueUnPayAmt());

        List<JdCreditBIllRepayDetailVo> detailList = new ArrayList<>(planDetailPos.size());
        for (LoanRepayPlanDetailPo planDetailPo : planDetailPos) {
            JdCreditBIllRepayDetailVo detailVo = new JdCreditBIllRepayDetailVo();
            detailVo.setTermNo(planDetailPo.getTermNo());
            detailVo.setCurrTerm(Integer.valueOf(1).equals(planDetailPo.getCurrTerm()) ? "是":"否");
            detailVo.setPlanStatus(ComEnums.LoanStatusEnum.getNameByCode(planDetailPo.getPlanStatus()));
            if (planDetailPo.getStartDate() != null) {
                detailVo.setStartDate(DatePattern.NORM_DATETIME_FORMAT.format(planDetailPo.getStartDate()));
            }
            if (planDetailPo.getEndDate() != null) {
                detailVo.setEndDate(DatePattern.NORM_DATETIME_FORMAT.format(planDetailPo.getEndDate()));
            }
            if (planDetailPo.getFinishDate() != null) {
                detailVo.setFinishDate(DatePattern.NORM_DATETIME_FORMAT.format(planDetailPo.getFinishDate()));
            }
            if (planDetailPo.getOverDueStartDate() != null) {
                detailVo.setOverDueStartDate(DatePattern.NORM_DATETIME_FORMAT.format(planDetailPo.getOverDueStartDate()));
            }
            detailVo.setPaidAmt(planDetailPo.getPaidAmt());
            detailVo.setCurrUnPayAmt(planDetailPo.getCurrUnPayAmt());
            detailVo.setDueUnPayAmt(planDetailPo.getDueUnPayAmt());
            detailList.add(detailVo);
        }
        billRepayVo.setDetailList(detailList);
        return billRepayVo;
    }
}
